package com.lhf.spareClass.controller;


import com.lhf.spareClass.model.*;
import com.lhf.spareClass.service.SpareService;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 尐痞猫
 * @date2021-06-25 22:48
 */
@RestController
@CrossOrigin
public class SpareController {

    @Autowired
    private SpareService spareService;

    //根据法人查询备件阀值下拉栏
    @GetMapping("/spare/queryByLegalPersonCode")
    public ResultVo queryByLegalPersonCode(String legalPersonCode) {
        return spareService.queryByLegalPersonCode(legalPersonCode);
    }
    @GetMapping("/spare/select")
    public ResultVo select(String legalPersonCode) {
        return spareService.select(legalPersonCode);
    }
    @GetMapping("/spare/queryByLegalPersonCode2")
    public ResultVo queryByLegalPersonCode2(String legalPersonCode) {
        return spareService.queryByLegalPersonCode2(legalPersonCode);
    }


    //模糊查询
    @GetMapping("/spare/likeCode")
    public ResultVo likeCode(SpareLikeQuery spareLikeQuery) {
        return spareService.likeCode(spareLikeQuery);
    }
    //条件查询
    @GetMapping("/spare/queryByCode")
    public ResultVo queryByCode(String code) {
        return spareService.queryByCode(code);
    }
  //备件小类下拉栏
    @GetMapping("/spare/subClass")
    public ResultVo subClass() {
        return spareService.subClass();
    }
   //备件小类下拉栏
    @PostMapping("/spare/queryPage")
    public ResultVo queryPage(@RequestBody SpareSelectVo spareSelectVo) {
        return spareService.queryPage(spareSelectVo);
    }
      //update
    @PostMapping("/spare/update")
    public ResultVo update(@Valid @RequestBody SpareUpdateVo spareUpdateVo, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareService.update(spareUpdateVo);
    }
    //add
    @PostMapping("/spare/add")
    public ResultVo add(@Valid @RequestBody SpareAddVo spareAddVo, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareService.add(spareAddVo);
    }

}
